﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.Extensions.Configuration;
using OA.Core.DataAccessHelper;
using OA.Core.Security;
using OA.Model;
using OA.Model.View;
using System.Collections.Generic;

namespace OA.Model
{

    public class VoucherDbContext : ExtendDbContext
    {
        public static IConfiguration Configuration;

        public VoucherDbContext()
        {
        }

        // step2:实现基类的构造方法，但可以什么都不干
        public VoucherDbContext(ICollection<TableMappingRule> rules) : base(rules)
        {

        }


        public static Dictionary<string, string> DicSql = new Dictionary<string, string>();

        // step3:把重写OnConfiguring的代码移到Configuring方法中进行重写
        protected override void Configuring(DbContextOptionsBuilder optionsBuilder)
        {
            string connectString = Configuration.GetConnectionString("MsSQL");
            if (!optionsBuilder.IsConfigured)
            {
                if (!DicSql.ContainsKey(Configuration.GetConnectionString("MsSQL")))
                {
                    string sqlConnectStr = DESHeper.DecryptDES(Configuration.GetConnectionString("MsSQL"), "Cjy20088");
                    DicSql.TryAdd(Configuration.GetConnectionString("MsSQL"), sqlConnectStr);
                }
                optionsBuilder.UseSqlServer(DicSql[Configuration.GetConnectionString("MsSQL")])
                  .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

            }
        }


        protected override void ModelCreating(ModelBuilder modelBuilder)
        {
            //throw new System.NotImplementedException();
        }



        

        /// <summary>
        /// 表单内容
        /// </summary>
        public virtual DbSet<Voucher> Voucher { get; set; }

        public virtual DbSet<ApprovalItem> Approval { get; set; }

        public virtual DbSet<ApprovalItem> ApprovalItem { get; set; }

        public virtual DbSet<CallBackInfo> CallBackInfo { get; set; }



    }
}
